package com.estudy.contorller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.estudy.entity.ResultVo;
import com.estudy.entity.User;
import com.estudy.service.UserService;
import com.estudy.util.MD5Utils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author: yi.jianhua
 */
@Controller
public class LoginController {

    private static final Logger logger = Logger.getLogger(LoginController.class);

    @Autowired
    private UserService userService;
    @Autowired

    @RequestMapping(value = "/")
    public String contextPage(Map<String, Object> map, HttpServletRequest request) {
        return "redirect:/index";
    }


    @RequestMapping(value = "/index")
    public String indexPage(Map<String, Object> map, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("u");
        if (user == null) {
            return "redirect:/login";
        }
        map.put("nickName", user.getNickName());
        return "index";
    }

    @RequestMapping(value = "/welcome")
    public String welcomePage(Map<String, Object> map, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("u");
        if (user == null) {
            return "redirect:/login";
        }
        map.put("nickName", user.getNickName());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = simpleDateFormat.format(new Date());
        map.put("now", now);
        return "welcome";
    }

    @RequestMapping(value = "/login")
    public String loginPage(Map<String, Object> map) {
        return "login";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("u");
        return "redirect:/login";
    }


    @RequestMapping(value = "/logValidate")
    @ResponseBody
    public String logValidate(String userName, String password, HttpServletRequest request) {
        ResultVo resultVo = new ResultVo();
        try {
            User user = userService.getUser(userName, password);
            if (user != null) {
                request.getSession().setAttribute("u", user);
                resultVo.setStatus(200);
                resultVo.setMessage("SUCCESS");
            } else {
                resultVo.setStatus(500);
                resultVo.setMessage("用户名密码错误");
            }

        } catch (Exception e) {
            logger.error("logValidate error", e);
            resultVo.setStatus(500);
            resultVo.setMessage("验证失败");
        }
        return JSON.toJSONString(resultVo, SerializerFeature.WriteMapNullValue);
    }

}
